package com.wang.exam.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.common.pojo.BasePojoEnum;
import com.wang.common.util.BeanUtil;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.exam.mapper.AtExamDescMapper;
import com.wang.exam.pojo.AtExamDesc;
import com.wang.exam.pojo.AtExamDescEnum;
import com.wang.exam.pojo.AtExamDescQuery;
import com.wang.exam.service.AtExamDescService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 针对表【考试_概要表】的数据库操作Service实现
 *
 * @author ZjxMi
 */
@Service
public class AtExamDescServiceImpl extends ServiceImpl<AtExamDescMapper, AtExamDesc>
        implements AtExamDescService {

    @Autowired
    private BeanUtil beanUtil;

    /**
     * model2: jsp --> servlet -->service --> dao --> db, 我们自己封装了一个 PageInfoUtil;
     * MybatisPlus: 不再需要调用 PageInfoUtil,老代码改造的时候是为了整合代码,如果没有老代码,直接写的新代码,采用下面的方式来搞
     *
     * @param page
     * @param query 查询对象
     * @return
     */
    @Override
    public List<AtExamDesc> findList(IPage<AtExamDesc> page, AtExamDescQuery query) {
        if (page != null) {
            return this.baseMapper.findList(page, query);
        }
        return this.baseMapper.findList(query);
    }

    @Override
    public AtExamDesc findOne(AtExamDescQuery query) {
        return this.baseMapper.findOne(query);
    }

    @Override
    public int updateBatch(AtExamDescQuery query) {
        return this.baseMapper.updateBatch(query);
    }

    @Override
    public int deleteBatch(AtExamDescQuery query) {
        return this.baseMapper.deleteBatch(query);
    }

    /**
     * 对数据额外处理(非用户填写的数据)
     * @param entity
     */
    private void proccedUpdateData(AtExamDesc entity){
        /* 检查地址不能重复 */
        AtExamDescQuery examDescParamQuery = new AtExamDescQuery();
        AtExamDesc examDescEntity = examDescParamQuery.getEntity();
        examDescEntity.setAddress(entity.getAddress());
        IPage<AtExamDesc> examDescPage = new Page<>(1, ConstatFinalUtil.SECOND);
        List<AtExamDesc> examDescList = this.findList(examDescPage, examDescParamQuery);
        if (!examDescList.isEmpty()) {
            boolean existsFlag = true;
            AtExamDesc resultDb = examDescList.get(0);
            if (Objects.isNull(entity.getId())) {
                /* 说明是保存 */
                if (Objects.nonNull(resultDb)) {
                    existsFlag = false;
                }
            } else {
                if (Objects.nonNull(resultDb)) {
                    /* 更新 */
                    existsFlag = resultDb.getId().equalsIgnoreCase(entity.getId());
                }
            }
            /* 邮箱重复,不能再往下进行,需要抛出异常 */
            Assert.isTrue(existsFlag,"考试地址不能重复");
        }

        if (Objects.isNull(entity.getCreateTime())) {
            entity.setCreateTime(new Date());
        }
        entity.setUpdateTime(new Date());
        /* 判断对象是否为空 */
        if (Objects.isNull(entity.getPubTime())) {
            entity.setPubTime(new Date());
        }
        /* 判断是否是周末 */
        if (Objects.nonNull(entity.getCurrDate())) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(entity.getCurrDate());
            int dayNum = calendar.get(Calendar.DAY_OF_WEEK);
            if (dayNum == Calendar.SUNDAY || dayNum == Calendar.SATURDAY) {
                entity.setDateStatus(AtExamDescEnum.DATESTATUS_WEEKEND.getCode());
            } else {
                entity.setDateStatus(AtExamDescEnum.DATESTATUS_WORK.getCode());
            }
        }

        if (AtExamDescEnum.EXAMSTATUS_END.getCode().equalsIgnoreCase(entity.getExamStatus())) {
            //TODO 考试的状态为:已结束,有后续的业务逻辑
        }
    }

    @Override
    public boolean save(AtExamDesc entity) {
        this.proccedUpdateData(entity);
        /* 新增加的记录默认未删除 */
        entity.setDelFlag(BasePojoEnum.DELFLAG_NO.getCode());
        entity.setStatus(AtExamDescEnum.STATUSNEW_INPUT.getCode());
        entity.setExamStatus(AtExamDescEnum.EXAMSTATUS_UNDO.getCode());
        return super.save(entity);
    }

    @Override
    public boolean updateById(AtExamDesc entity) {
        /* 先根据id查询数据库的内容; */
        AtExamDescQuery paramQuery = new AtExamDescQuery();
        AtExamDesc entityParam = paramQuery.getEntity();
        entityParam.setId(entity.getId());
        /* 数据库中查询的结果 */
        AtExamDesc oneDbResult = this.findOne(paramQuery);
        /* 更新数据库信息 */
        AtExamDesc examDescDb = new AtExamDesc();
        this.beanUtil.copy(oneDbResult, examDescDb);
        this.beanUtil.copy(entity, examDescDb);
        /*
        * 源对象和目标对象是一个类型(10个字段)
        * 源对象有5个字段有值,目标对象是10个有值
        * 把源对象中的5个字段有值,赋值到目标对象中.(覆盖)
        * */
        examDescDb = (AtExamDesc) this.beanUtil.copy(entity, examDescDb);

        this.proccedUpdateData(examDescDb);
        return super.updateById(examDescDb);
    }
}